//
//  HTSingleton.m
//  HappyTravel
//
//  Created by JiePan’s Mac on 2018/8/6.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "HTSingleton.h"

@implementation HTSingleton

+ (instancetype)shareInstance {
    static HTSingleton *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[[self class]alloc]init];
    });
    return instance;
}

+ (void)alert:(NSString *)message  Con:(UIViewController *)controller{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hint" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    [alertController addAction:action];
    
    [controller presentViewController:alertController animated:YES completion:^{
    }];
}

+ (NSString *)getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

+ (NSString *)getCurrentDay {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

+ (NSString *)getCurrentMonth {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

+ (NSString *)getCurrentYear {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

/** 保存数据的路径 */
+ (NSString *)createFullPathForDocument:(NSString *)path {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path];
    
    return fullPath;
}

/** 从路径中获取保存model数组 */
+ (NSMutableArray *)readModelArrayForDocument:(NSString *)path {
    
    NSMutableArray *createCodeArray = [[NSMutableArray alloc] init];
    NSString *fullPath = [self createFullPathForDocument:path];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath])//判断dirPath路径文件夹是否已存在，此处dirPath为需要新建的文件夹的绝对路径
    {
        //根据路径读出可变data
        NSData *data = [NSData dataWithContentsOfFile:fullPath];
        //根据mutableData创建反序列化器
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        //反序列化器解码
        createCodeArray = [unarchiver decodeObjectForKey:@"model"];
        //反序列化器结束解码
        [unarchiver finishDecoding];
        
    }else {
        [[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil];
    }
    
    
    return createCodeArray;
}

/** 保存数据到沙盒 */
+ (void)saveModelDictionaryInDocument:(NSMutableArray *)array andPath:(NSString *)path {
    
    // ***********归档，序列化****************
    // 1。创建一个可变的二进制流
    NSMutableData *data = [[NSMutableData alloc] init];
    // 2。创建一个归档对象（有将自定义类转化为二进制流的功能）
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // 3。用该归档对象，把自定义类的对象，转为二进制流
    [archiver encodeObject:array forKey:@"model"];
    // 4归档完毕
    [archiver finishEncoding];
    
    //将数据写入文件
    NSString *fullPath = [self createFullPathForDocument:path];
    [data writeToFile:fullPath atomically:YES];
}


+ (CGSize)getSingleSize:(CGSize)singleSize
{
    CGFloat max_width = ScreenWidth - 150;
    CGFloat max_height = ScreenHeight - 130;
    CGFloat image_width = singleSize.width;
    CGFloat image_height = singleSize.height;
    
    CGFloat result_width = 0;
    CGFloat result_height = 0;
    if (image_height/image_width > 3.0) {
        result_height = max_height;
        result_width = result_height/2;
    }  else  {
        result_width = max_width;
        result_height = max_width*image_height/image_width;
        if (result_height > max_height) {
            result_height = max_height;
            result_width = max_height*image_width/image_height;
        }
    }
    return CGSizeMake(result_width, result_height);
}




@end
